package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.drink.DrinkMgr;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;

public class DunHuangRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = DunHuangShiKuMgr.activityInfo;
        if (activityInfo != null) {
            long recycleTime = activityInfo.getEndShowTime() * 1000L + 600 * 1000;
            for (Integer itemId : DunHuangShiKuMgr.consumeIdSet) {
                ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleTime);
            }
            for (Integer itemId : DunHuangShiKuMgr.coloringIdList) {
                ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, 1, recycleTime, new ArrayList<>());
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        ActivityInfo activityInfo = DunHuangShiKuMgr.activityInfo;
        return ActivityMgr.activityInTime(activityInfo);
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.CrossDunHuangShiKuActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        if (recycleType == 1) {
            recycleColorItem(userId, data);
        } else {
            recycleConsumeItem(userId, data);
        }
    }

    private void recycleConsumeItem(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId, itemId);
        if (userBagItem != null && userBagItem.getNum() > 0) {
            removeItem(userBagItem);
        }
    }

    private void recycleColorItem(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        Property property = new Property();
        UserBagItem userBagItem = getUserBagItem(userId, itemId);
        String language = UserMgr.getUserInfo(userId).getLanguage();
        if (userBagItem != null && userBagItem.getNum() > 0) {
            for (int i = 0; i < userBagItem.getNum(); i++) {
                OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(userBagItem.getGoodsId());
                property.addProperty(openGoodsBagResult.getReward());
            }
            removeItem(userBagItem);
        }
        if (property.getGoods().size() > 0) {
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), MultipleLanguageMgr.getContent(MailManager.DUNHUANGSHIKU_RETURN_COLORING_ITEM_CONTENT, language),
                    MultipleLanguageMgr.getContent(MailManager.DUNHUANGSHIKU_RETURN_COLORING_ITEM_TITLE, language));
        }
    }
}
